/*
 * TCSS 422 - Spring quarter 2011
 * Team: 	Googlers
 * Members: Deepa Sahni, Krisnil Gounder, Michael Schweiger
 * Date: 	April 7, 2011
 */
package testing;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.net.MalformedURLException;
import java.net.URL;

import org.junit.Before;
import org.junit.Test;

import buffers.URLBuffer;


/**
 * This is a simple test case class using JUnit4.
 * @author Michael Schweiger
 * @version 1.0
 */
public class URLBufferUnitTest {

	/**
	 * The string for google.
	 */
	private final String google = "HTTP://www.google.com";
	
	/**
	 * The string for yahoo.
	 */
	private final String yahoo = "HTTP://www.yahoo.com";
	
	/**
	 * The PageBuffer test fixture.
	 */
	private URLBuffer buffer;
	
	/**
	 * Initialize the test fixtures.
	 */
	@Before
	public void setUpBeforeClass() {
		buffer = URLBuffer.getInstance();
	}
	
	/**
	 * Tests to verify that two instances of PageBuffer are the same (test singleton).
	 */
	@Test
	public void testSingleton() {
		URLBuffer[] buffer_array = new URLBuffer[100];
		for (int i = 0; i < buffer_array.length; i++) {
			buffer_array[i] = URLBuffer.getInstance();
		}
		for (int i = 1; i < buffer_array.length; i++) {
			assertSame(buffer_array[i - 1], buffer_array[i]);
		}
	}
	
	/**
	 * Tests adding a new unseen URL to the buffer.
	 */
	@Test
	public void testAddUnseen() {
		
		//get prior size and seen count
		final int prior_size = buffer.getSize();
		final int prior_size_seen = buffer.getSeenCount();
		
		//initialize the URL to be added
		URL googleURL = null;
		try {
			googleURL = new URL(google);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			fail("URL didn't initialize properly before adding!");
		}
		
		//add the URL and verify that size and seen count incremented.
		assertTrue(buffer.add(googleURL));
		assertEquals(prior_size + 1, buffer.getSize());
		assertEquals(buffer.getSize(), URLBuffer.getInstance().getSize());
		assertEquals(prior_size_seen + 1, buffer.getSeenCount());
	}
	
	/**
	 * Tests adding a seen URL.
	 */
	@Test
	public void testSeenURL() {
		//get prior sizes
		int prior_size = buffer.getSize();
		int prior_seen_count = buffer.getSeenCount();
		
		//initialize the URL to be added.
		URL yahooURL= null;
		try {
			yahooURL = new URL(yahoo);
		} catch (MalformedURLException e) {
			e.printStackTrace();
			fail("URL didn't initialize properly");
		}
		
		//add the URL and verify that size and seen count incremented.
		//This time yahooURL hasn't been seen yet.
		assertTrue(buffer.add(yahooURL));
		assertEquals(++prior_size, buffer.getSize());
		assertEquals(buffer.getSize(), URLBuffer.getInstance().getSize());
		assertEquals(++prior_seen_count, buffer.getSeenCount());
		
		//Attempt to add the url again.
		assertFalse(buffer.add(yahooURL));
		assertEquals(prior_size, buffer.getSize());
		assertEquals(buffer.getSize(), URLBuffer.getInstance().getSize());
		assertEquals(prior_seen_count, buffer.getSeenCount());
	}
}
